/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Itens.java
 *
 * Created on 04/03/2013, 10:57:29
 */
package gui;


import java.util.ArrayList;

import java.util.Arrays;
import java.util.Comparator;
import javax.swing.JPanel;
import javax.swing.JSlider;
import visual.ontogrator.pkg2.Gene;


/**
 *
 * @author Gabriel
 */



public class LCSFilter extends javax.swing.JFrame  {
  ArrayList<Integer> verif = new ArrayList<>();

    public ArrayList<Integer> getVerif() {
        return verif;
    }

    public void setVerif(ArrayList<Integer> verif) {
        this.verif = verif;
    }
  ArrayList<Integer> selected = new ArrayList<>();
  ArrayList<JSlider> asld = new ArrayList<>();

 
  float[] vcorte;
  int[] palette;
  
     public float[] getVcorte() {
        return vcorte;
    }

    public void setVcorte(float[] vcorte) {
        this.vcorte = vcorte;
    }
    
  
    public int[] getPalette() {
        return palette;
    }

    public void setPalette(int[] palette) {
        this.palette = palette;
    }
    public ArrayList<Integer> getSelected() {
        return selected;
    }

    public void setSelected(ArrayList<Integer> selected) {
        this.selected = selected;
    }

  ArrayList<Gene> agene;

    public ArrayList<Gene> getAgene() {
        return agene;
    }

    public void setAgene(ArrayList<Gene> agene) {
        this.agene = agene;
    }
   
    /** Creates new form Itens */
 
  

    public LCSFilter() {
        initComponents();
       
    }

   

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Set Filters");
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setText("Selecione a porcentagem do filtro LCS para gerar a rede:");
        jPanel4.add(jLabel1);

        getContentPane().add(jPanel4);

        jScrollPane1.setToolTipText("");
        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 300));

        jPanel1.setLayout(new java.awt.GridLayout(10, 1, 5, 5));
        jScrollPane1.setViewportView(jPanel1);

        getContentPane().add(jScrollPane1);

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1);

        jButton2.setText("Atualizar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton2);

        getContentPane().add(jPanel3);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    public void atualizar(){
        verif.clear();
    asld.clear();
    jPanel1.removeAll();
    ArrayList<String> atitulos = new ArrayList<>();
    ArrayList<JPanel> ajpanel = new ArrayList<>();
  
    //percorre array de genes
    for(int i=0;i<agene.size();i++){
        //percorre as informações do gene
        for(int j=0;j<agene.get(i).getArrayInfo().size();j++){
            //verifica se a informação nao esta vazia
            if(!(agene.get(i).getArrayInfo().get(j).isEmpty())){
                //se não estiver vazia guardar informação nao vazia no vetor verif
                if(!(verif.contains(j))){
                    //criar jpanel com a informação não vazia
                     
                    verif.add(j);
                    LCSSlider jpslider = new LCSSlider();
                    jpslider.setVisible(true);
                    atitulos.add(agene.get(i).getArrayTitulo().get(j));
                    jpslider.setjLabelText(agene.get(i).getArrayTitulo().get(j));
                    
                
                    asld.add(jpslider.getjSlider1());
                    ajpanel.add(jpslider);
                }
            }
        }
     }
    
    
    //deixar em ordem alfabética
    
    //criar um array onde serão guardados os titulos que serão ordenados e os indexs
    String[][] titulos = new String[atitulos.size()][];
    
    //preencher  o array[][] com os titulos e os index
    for(int i=0;i<ajpanel.size();i++){
        String[] aux = {atitulos.get(i),i+""};
        titulos[i] = aux; 
    }
    
    //ordem com base nos titulos somente
    Arrays.sort(titulos, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String p1 = entry1[0];
                final String p2 = entry2[0];
                return p1.compareTo(p2);
            }
        });
    
    //adicionar os itens de acordo com os indexs ordenados de acordo com os titulos
    for (final String[] s : titulos) {
        //System.out.println(s[0] + " " + s[1]);
        int index = Integer.parseInt(s[1]);
        jPanel1.add(ajpanel.get(index));
    }
     jPanel1.setLayout(new java.awt.GridLayout(asld.size(), 1, 5, 5));
     jPanel1.revalidate();
     jPanel1.repaint();
    }
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    
    atualizar();
    
}//GEN-LAST:event_jButton2ActionPerformed

public int[] getValores(){
    int valores[] = new int[asld.size()];
    for(int i=0;i<valores.length;i++){
          valores[i] = asld.get(i).getValue();
    }
    return valores;
}

public void setValores(int[] valores){
    for(int i=0;i<valores.length;i++){
          asld.get(i).setValue(valores[i]);
    }
}

public void salvar(){
    selected.clear();
    Gene gene = new Gene();
    vcorte = new float[gene.getArrayInfo().size()];
    for (int i = 0; i < asld.size(); i++) {
        
            //adicionar index dos itens selecionado ao array selected
        //System.out.println("valor sem dividir por 100 " + asld.get(i).getValue());
        int valorsld = asld.get(i).getValue();
        if (valorsld > 0) {
            float aux = (float) valorsld;
            vcorte[verif.get(i)] = (float) (aux / 100);
        } else {
            vcorte[verif.get(i)] = (float) 0.0;
        }
                
       //System.out.println("Filtro LCS da info "+gene.getArrayTitulo().get(verif.get(i))+" é "+vcorte[verif.get(i)]);
        
    }
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    
   salvar();
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LCSFilter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LCSFilter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LCSFilter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LCSFilter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
               new LCSFilter().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
